package com.tcp2.frame;

import com.google.common.collect.Maps;
import com.google.common.util.concurrent.*;

import io.netty.channel.nio.NioEventLoopGroup;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executors;

/**
 * Author: <张锦煌>
 * Time: 2015-09-15 14:40
 * Copyright (C) 2015 Xiamen Yaxon Networks CO.,LTD.
 */

/*
平台通用版操作界面
 */
public class PlatFrame extends JFrame implements  ActionListener {
    private ListeningScheduledExecutorService timerService;
    private  Vector vector0=new Vector();
    private   JTable table=new JTable();
   // private LoadFile loadFile=new LoadFile();
    public static ConcurrentMap<Long,Integer> taskIdMap2= Maps.newConcurrentMap();
   // private PassThroughHA passThroughHA=new PassThroughHA(this);

    private JTextField rateT=new JTextField("30",5);
    private NioEventLoopGroup ioThreadPool = new NioEventLoopGroup();
  //  final ServerConfig serverConfig;
    //final JTextField addressT;
    //final JTextField portT;
    final JLabel jl1=new JLabel();


    final JRadioButton jr1=new JRadioButton("成功",true);
    JRadioButton jr2=new JRadioButton("取消");
    final JRadioButton jr3=new JRadioButton("预约电召开始执行");


    public PlatFrame(JPanel jp) throws Exception {
        //服务器连接
        JPanel jp0 = new JPanel();
        JPanel jp1 = new JPanel();
        JPanel jp5 = new JPanel();
        TitledBorder connBorder = new TitledBorder("服务器连接");
        TitledBorder sctBorder = new TitledBorder("选择操作");
        jp1.setBorder(connBorder);
        jp5.setBorder(sctBorder);
        jp0.add(jp1);
        jp0.add(jp5);
        JLabel address = new JLabel("IP地址");
        JLabel port = new JLabel("TCP端口号");
        //   serverConfig=loadFile.loadServerConfig("./conf/app.properties");
        //    addressT = new JTextField(serverConfig.getAddress(), 15);
        //    portT = new JTextField(serverConfig.getPort(), 10);
        JButton editB = new JButton("保存");
        jp1.add(address);
        //  jp1.add(addressT);
        jp1.add(port);
        // jp1.add(portT);
        jp1.add(editB);
        editB.addActionListener(this);


        //选择操作
        JButton sct1 = new JButton("全选");
        JButton sct2 = new JButton("取消");
        JButton sct3 = new JButton("在线");
        JButton sct6 = new JButton("运行");
        JButton sct4 = new JButton("离线");
        JButton sct7 = new JButton("任务中");
        JButton sct5 = new JButton("未注册");
        jp5.add(sct1);
        jp5.add(sct2);
        jp5.add(sct5);
        jp5.add(sct3);
        jp5.add(sct4);
        jp5.add(sct6);
        jp5.add(sct7);

        //创建表格数据
        JPanel jp2 = new JPanel();
        TitledBorder msgBorder = new TitledBorder("车台信息");
        jp2.setBorder(msgBorder);
        jp2.setLayout(new BorderLayout());

        jl1.setText("*选中车辆数：【" + String.valueOf(table.getSelectedRowCount()) + "】");

        //判断数据来源
        /*if(serverConfig.getDataSource()==0){
            VehicleConfig config = null;
            try {
                config = loadFile.loadConfig();
            } catch (Exception e1) {
                System.out.println(e1);
            }
            if (config == null) {
            } else {
                try {
                    int s4 = config.getCount();
                    vector0 = loadFile.loadConfigInfo(s4);
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }else{
            try {
                vector0=loadFile.readerCsv("./conf/veInfoExp.csv");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*/

        // SwingUtil.createTableData(vector0, table);    //载入数据到表格

        table.setPreferredScrollableViewportSize(new Dimension(650, 380));
        JScrollPane s = new JScrollPane(table);
        jp2.add(jl1, "North");
        jp2.add(s, "Center");
        //  MyQuene.addTable(table);


        sct1.addActionListener(this);
        sct2.addActionListener(this);
        sct3.addActionListener(this);
        sct4.addActionListener(this);
        sct5.addActionListener(this);
        sct6.addActionListener(this);
        sct7.addActionListener(this);


        //鼠标点击改变表格列选中触发事件
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                jl1.setText("*选中车辆数：【" + String.valueOf(table.getSelectedRowCount()) + "】");
            }
        });

        //终端操作
        JPanel jp3 = new JPanel();
        TitledBorder optBorder = new TitledBorder("终端操作");
        jp3.setBorder(optBorder);
        JButton btn1 = new JButton("车台注册");
        JButton btn2 = new JButton("车台鉴权");
        JButton btn3 = new JButton("车台注销");
        JButton btn4 = new JButton("更多操作");
        JPanel gpsJ = new JPanel();
        TitledBorder gpsBorder = new TitledBorder("定时任务");
        gpsJ.setBorder(gpsBorder);
        final JRadioButton jt1 = new JRadioButton("GPS", true);
        JRadioButton jt2 = new JRadioButton("图片");
        ButtonGroup bg0 = new ButtonGroup();
        bg0.add(jt1);
        bg0.add(jt2);
        JLabel timeRate = new JLabel("间隔");
        JLabel dw = new JLabel("秒");
        JButton btn51 = new JButton("发送");
        JButton btn52 = new JButton("停止");
        JButton btn9 = new JButton("批量GPS");
        Box g0 = Box.createHorizontalBox();
        g0.add(jt1);
        g0.add(jt2);
        Box g1 = Box.createHorizontalBox();
        g1.add(Box.createHorizontalStrut(10));
        g1.add(timeRate);
        g1.add(rateT);
        g1.add(dw);
        g1.add(Box.createHorizontalStrut(20));
        Box g2 = Box.createHorizontalBox();
        g2.add(btn51);
        g2.add(btn52);
        Box g3 = Box.createHorizontalBox();
        g3.add(btn9);
        Box vg = Box.createVerticalBox();
        vg.add(g0);
        vg.add(g1);
        vg.add(g2);
        vg.add(Box.createVerticalStrut(5));
        vg.add(g3);
        gpsJ.add(vg);

        Box b1 = Box.createHorizontalBox();
        b1.add(btn1);
        Box b2 = Box.createHorizontalBox();
        b2.add(btn2);
        Box b3 = Box.createHorizontalBox();
        b3.add(btn3);
        Box b4 = Box.createHorizontalBox();
        b4.add(btn4);
        Box b5 = Box.createHorizontalBox();
        b5.add(gpsJ);
        Box vb = Box.createVerticalBox();
        vb.add(b1);
        vb.add(Box.createVerticalStrut(5));
        vb.add(b2);
        vb.add(Box.createVerticalStrut(5));
        vb.add(b3);
        vb.add(Box.createVerticalStrut(5));
        vb.add(b4);
        vb.add(Box.createVerticalStrut(5));
        vb.add(b5);
        jp3.add(vb);

        //出租车电召业务
        JPanel jp4 = new JPanel();
        TitledBorder taxiCallBorder = new TitledBorder("电召业务");
        jp4.setBorder(taxiCallBorder);

        // btn5.setEnabled(false);
        JPanel callJ = new JPanel();
        TitledBorder callBorder = new TitledBorder("电召结果");
        callJ.setBorder(callBorder);
        //   jp4.setVisible(false);


        //出租车电召业务

        ButtonGroup bg = new ButtonGroup();
        bg.add(jr1);
        bg.add(jr2);
        bg.add(jr3);
        Box c1 = Box.createHorizontalBox();
        c1.add(jr1);
        c1.add(jr2);
        Box c2 = Box.createHorizontalBox();
        c2.add(jr3);
        Box c3 = Box.createHorizontalBox();
        // c3.add(btn6);
        // c3.add(btn71);
        Box vc = Box.createVerticalBox();
        vc.add(c1);
        vc.add(c2);
        vc.add(c3);
        callJ.add(vc);
        JButton btn7 = new JButton("订单完成");

        Box b6 = Box.createHorizontalBox();
        // b6.add(btn5);b6.add(btn8);
        Box b7 = Box.createHorizontalBox();
        b7.add(callJ);
        Box b8 = Box.createHorizontalBox();
        b8.add(btn7);
        Box cvb = Box.createVerticalBox();
        cvb.add(b6);
        vb.add(Box.createVerticalStrut(5));
        cvb.add(b7);
        cvb.add(b8);
        jp4.add(cvb);

        Box op = Box.createVerticalBox();
        op.add(jp3);
        op.add(jp4);


        btn1.addActionListener(new ActionListener() {     //终端注册
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean flag = false;
                if (table.getSelectedRowCount() <= 0) {
                    JOptionPane.showMessageDialog(null, "请至少选择一辆车台！");
                } else {
                    for (int rowindex : table.getSelectedRows()) {
                        String state = (String) table.getValueAt(rowindex, 3);
                        if (!(state.equals("未注册"))) {
                            JOptionPane.showMessageDialog(null, "有车辆已注册！");
                            flag = false;
                            break;
                        } else {
                            flag = true;
                        }
                    }
                    if (flag) {
                        //      final Connection c = new Connection(ioThreadPool);
                        timerService = MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(8));
                        for (final int rowindex : table.getSelectedRows()) {
                            final long sim = Long.parseLong((String) table.getValueAt(rowindex, 0));
                            final String lpn = (String) table.getValueAt(rowindex, 1);
                            final String tid = (String) table.getValueAt(rowindex, 2);
                            final SettableFuture<Integer> future = SettableFuture.create();
                            //   final Connection c = new Connection();
                         /*   ListenableFuture<Object> listenableFuture=timerService.submit(new Callable<Object>() {
                                @Override
                                public Object call() throws Exception {
                                    c.setAddress(addressT.getText());
                                    c.setPort(portT.getText());
                                    c.conn(sim, lpn, tid, 0, "", future);
                                    return null;
                                }
                            });*/
                      /*      Futures.addCallback(listenableFuture, new FutureCallback<Object>() {
                                @Override
                                public void onSuccess(Object o) {
                                    int state = 0;
                                    try {
                                        state = future.get();
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                    if (state == ChannelHA.ST_TO_REGISTER) {
                                        table.setValueAt("离线", rowindex, 3);
                                    }
                                    if (state == ChannelHA.ST_TO_UNREGISTER) {
                                        JOptionPane.showMessageDialog(null, "注册失败！");
                                    }
                                }

                                @Override
                                public void onFailure(Throwable throwable) {
                                    return;
                                }
                            });*/
                        }
                    }
                }
            }
        });

        btn2.addActionListener(new ActionListener() {      //车辆上线
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean flag = false;
                if (table.getSelectedRowCount() <= 0) {
                    JOptionPane.showMessageDialog(null, "请至少选择一辆车台！");
                } else {
                    for (int rowindex : table.getSelectedRows()) {
                        String state = (String) table.getValueAt(rowindex, 3);
                        if (!(state.equals("离线"))) {
                            JOptionPane.showMessageDialog(null, "有车辆未注册或已在线！");
                            flag = false;
                            break;
                        } else {
                            flag = true;
                        }
                    }
                    if (flag) {
                        timerService = MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(8));
                        for (final int rowindex : table.getSelectedRows()) {
                            final long sim = Long.parseLong((String) table.getValueAt(rowindex, 0));
                            final SettableFuture<Integer> future = SettableFuture.create();
                          /*  final Connection c = new Connection(ioThreadPool);
                            ListenableFuture<Object> listenableFuture=timerService.submit(new Callable<Object>() {
                                @Override
                                public Object call() throws Exception {
                                    String authcode= MyQuene.getAuthcode(sim);
                                    c.setAddress(addressT.getText());
                                    c.setPort(portT.getText());
                                    c.conn(sim, "", "", 1, authcode, future);
                                    return null;
                                }
                            });
                            Futures.addCallback(listenableFuture, new FutureCallback<Object>() {
                                @Override
                                public void onSuccess(Object o) {
                                    int state = 0;
                                    try {
                                        state = future.get();
                                    } catch (Exception e1) {
                                        e1.printStackTrace();
                                    }
                                    if (state == ChannelHA.ST_AUTHENTICATED) {
                                        table.setValueAt("在线", rowindex, 3);
                                    } else {
                                        JOptionPane.showMessageDialog(null, "鉴权失败！");
                                    }
                                }
                                @Override
                                public void onFailure(Throwable throwable) {
                                    return ;
                                }
                            });*/
                        }
                    }
                }
            }
        });

        btn3.addActionListener(new ActionListener() {     //车辆注销
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean flag = false;
                if (table.getSelectedRowCount() <= 0) {
                    JOptionPane.showMessageDialog(null, "请至少选择一辆车台！");
                } else {
                    for (int rowindex : table.getSelectedRows()) {

                        String state = (String) table.getValueAt(rowindex, 3);
                        if ((state.equals("未注册")) || (state.equals("运行"))) {
                            JOptionPane.showMessageDialog(null, "有车辆未注册或运行状态！");
                            flag = false;
                            break;
                        } else {
                            flag = true;
                        }
                    }
                    if (flag) {
                        timerService = MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(8));
                        for (final int rowindex : table.getSelectedRows()) {
                            String state0 = (String) table.getValueAt(rowindex, 3);
                            final long sim = Long.parseLong((String) table.getValueAt(rowindex, 0));

                        }
                    }
                }
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {

    }
}

